package com.saturn.titan.core.comm;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Maps;
import com.saturn.titan.core.bean.BaseArea;
import com.saturn.titan.core.bean.PlatformEmp;
import com.saturn.titan.core.bean.PlatformOrg;
import com.saturn.titan.core.repository.mybatis.BaseAreaDao;
import com.saturn.titan.core.repository.mybatis.PlatformEmpDao;
import com.saturn.titan.core.repository.mybatis.PlatformOrgDao;

public class CRMCache {

	@Autowired
	private PlatformEmpDao empDao;
	
	@Autowired
	private PlatformOrgDao orgDao;
	
	@Autowired
	private BaseAreaDao baseAreaDao;
	
	public static final Map<Long,PlatformEmp> EMP_CACHE = Maps.newHashMap();
	public static final Map<Long,PlatformOrg> ORG_CACHE = Maps.newHashMap();
	public static final Map<Long,BaseArea> AREA_CACHE = Maps.newHashMap();
	
	public void cacheInit(){
		
		List<PlatformEmp> result = empDao.select(new PlatformEmp());
		for(PlatformEmp e : result){
			EMP_CACHE.put(e.getId(),e);
		}
		
		List<PlatformOrg> resultOrg = orgDao.select(new PlatformOrg());
		for(PlatformOrg o : resultOrg){
			ORG_CACHE.put(o.getId(),o);
		}
		
		List<BaseArea> resultArea = baseAreaDao.select(new BaseArea());
		for(BaseArea o : resultArea){
			AREA_CACHE.put(o.getId(),o);
		}
	}
	
	public static String displayOpr(Long empId){
		if(empId == null){
			return "";
		}
		if(EMP_CACHE.get(empId) == null){
			return "";
		}
		
		return EMP_CACHE.get(empId).getEmpName()+"["+EMP_CACHE.get(empId).getEmpNo()+"]";
	}
	
	public static String displayAccount(Long empId){
		if(empId == null){
			return "";
		}
		if(EMP_CACHE.get(empId) == null){
			return "";
		}
		
		return EMP_CACHE.get(empId).getAccount();
	}
	
	public static String displayOrg(Long orgId){
		if(orgId == null){
			return "";
		}
		if(ORG_CACHE.get(orgId) == null){
			return "";
		}
		
		return ORG_CACHE.get(orgId).getOrgNameSimple();
	}

	public static String displayArea(Long areaId) {
		
		if(areaId == null){
			return "";
		}
		if(AREA_CACHE.get(areaId) == null){
			return "";
		}
		
		return AREA_CACHE.get(areaId).getAreaName();
	}
}
